from django.contrib import admin
from .models import User, UserRole, Role
from system.admin import CommonInfoAdmin
from system.models import RoleMenu

# Register your models here.
class UserRoleInline(admin.TabularInline):
    model = UserRole
    # 默认不额外显示
    extra = 0
    verbose_name = '用户角色'
    verbose_name_plural = '用户角色'
    # 设置列表显示字段
    fields = ('role', 'whether_main', 'data_remark', 'create_user', 'create_time', 'update_user', 'update_time', 'deleted_time')
    # 设置只读字段
    readonly_fields = ('create_user', 'create_time', 'update_user', 'update_time', 'deleted_time')

@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    # 设置列表显示字段
    list_display = ('username', 'nickname', 'gender', 'birthday', 'email', 'avatar')
    # 设置过滤器字段
    list_filter = ('gender', 'is_staff', 'is_superuser', 'is_active')
    # 设置可搜索字段
    search_fields = ('username', 'nickname', 'gender')
    # 内联显示字段
    inlines = [UserRoleInline]

class RoleMenuInline(admin.TabularInline):
    model = RoleMenu
    # 默认不额外显示
    extra = 0
    verbose_name = '角色菜单'
    verbose_name_plural = '角色菜单'
    # 设置列表显示字段
    fields = ('menu', 'data_remark', 'create_user', 'create_time', 'update_user', 'update_time', 'deleted_time')
    # 设置只读字段
    readonly_fields = ('create_user', 'create_time', 'update_user', 'update_time', 'deleted_time')


@admin.register(Role)
class RoleAdmin(CommonInfoAdmin):
    # 设置列表显示字段
    list_display = ('id','name', 'description', 'remark','sort_num')
    # 设置过滤器字段
    list_filter = ('valid_status', 'be_deleted')
    # 设置可搜索字段
    search_fields = ('name', 'description', 'remark')
    # 设置编辑页面显示字段
    fields = ('name', 'description','remark','sort_num', 'data_remark', 'get_create_user_info', 'get_update_user_info', 'update_time', 'be_deleted', 'deleted_time', 'valid_status')
    # 设置排序字段
    ordering = ('sort_num', 'id')
    # 内联显示字段
    inlines = [RoleMenuInline]
